Repository navigation
Distributor: add client-identity ingestion rate limiting - #7947
krunaljain wants to merge 3 commits into
Conversation
Add an optional per-(tenant, X-Scope-ClientID) ingestion rate limit below the tenant-level limit, as specified in docs/proposals/client-identity-ingestion-rate-limiting.md. Signed-off-by: Krunal Jain <krunal.bitsy@gmail.com>
| // today, exactly as the proposal requires. | ||
| if clientLimit := limits.ClientIdentityIngestionRateLimit; clientLimit > 0 { | ||
| if clientID := clientid.FromContext(ctx); clientID != "" { | ||
| if !d.clientIdentityLimiter.AllowN(now, userID, clientID, totalN) { |
There was a problem hiding this comment.
This 429 still spends tenant tokens. Can we leave that budget available for other writers when a client hits its own limit?
There was a problem hiding this comment.
Done. The client-identity check now runs before the tenant limiter, so a client rejected on its own limit never touches tenant tokens. Added a test where one client is rejected repeatedly and another still gets the full tenant budget.
| } | ||
|
|
||
| capacity := int64(limit*g.windowDuration.Seconds()) + int64(burst) | ||
| if cs.localCount+cs.peerSum+int64(n) > capacity { |
There was a problem hiding this comment.
How does peerSum expire after a peer goes quiet? Rejected requests never set dirty, so pruning can stop, and applyPeerView skips clients missing from the desc, so a pruned client keeps its old peerSum and stays at 429.
There was a problem hiding this comment.
Done. Peers are now stored per replica and summed in AllowN, skipping any counter whose LastUpdateUnixNano is older than retention. A quiet peer stops counting without needing another gossip callback or a dirty flag. applyPeerView also clears peers for tracked clients missing from the desc.
| // by pruning entries whose LastUpdateUnixNano has aged out (see GossipLimiter's | ||
| // prune loop), not by treating "absent from this particular gossip message" as | ||
| // "gone". | ||
| func (d *ClientCounterDesc) Merge(mergeable memberlist.Mergeable, _ bool) (memberlist.Mergeable, error) { |
There was a problem hiding this comment.
Do pruned clients disappear from memberlist? The merge keeps old entries that are missing from the update, so dead replicas keep counting toward peerSum (one rolling restart can block a client for good).
There was a problem hiding this comment.
Merge never deletes, so I implemented ClientCounterDesc.RemoveTombstones to drop replica counters older than the limit (and empty clients). The read-side retention filter above means correctness doesn't depend on how often memberlist calls it. Added tests for both.
| store: memberlist | ||
| ``` | ||
|
|
||
| The KV client is created lazily, the first time a tenant actually exercises the feature, so deployments that never opt in start no KV client or watch. On the write path, `X-Scope-ClientID` is read from the HTTP headers (remote write, OTLP over HTTP) or the gRPC metadata (OTLP over gRPC). |
There was a problem hiding this comment.
The OTLP/HTTP handler doesn't read X-Scope-ClientID. Can we read it there or drop OTLP/HTTP from this list?
There was a problem hiding this comment.
Done. OTLPHandler now injects X-Scope-ClientID the same way the remote write handler does, so the doc stays accurate. Added TestOTLPHandler_InjectsClientID.
…t ID - Check the client-identity limit before the tenant limiter so a rejected client does not spend tenant tokens. - Track peer counters per replica and ignore ones older than retention at admission time, so quiet or dead replicas stop counting. Clear peers for clients missing from the gossiped view. - Implement ClientCounterDesc.RemoveTombstones to drop stale replica counters from the merged memberlist state. - Read X-Scope-ClientID in the OTLP/HTTP handler. Signed-off-by: Krunal Jain <krunal.bitsy@gmail.com>
Signed-off-by: Krunal Jain <krunal.bitsy@gmail.com> # Conflicts: # pkg/distributor/distributor_test.go
What this PR does:
Adds an optional, experimental per-(tenant,
X-Scope-ClientID) ingestion rate limit enforced in the distributor, below the existing tenant-level limit. It implements the write-path proposal merged in #7681 (docs/proposals/client-identity-ingestion-rate-limiting.md, updated in this PR).client_identity_ingestion_rate_limit,client_identity_ingestion_burst_size,client_identity_ingestion_tracked_clients_limit. Defaults of 0 disable the check, so there is no behavior change unless enabled.X-Scope-ClientIDheader set by the same gateway/proxy that setsX-Scope-OrgID; supported on the push and OTLP gRPC paths.The query-path limit is a separate follow-up.
AI disclosure (per GENAI_POLICY.md): this change was prepared with an AI coding assistant (Claude Code), and reviewed and tested by the author.
Which issue(s) this PR fixes:
N/A
Checklist
CHANGELOG.mdupdated - the order of entries should be[CHANGE],[FEATURE],[ENHANCEMENT],[BUGFIX]docs/configuration/v1-guarantees.mdupdated if this PR introduces experimental flags